package com.sfh.cpc.com.ui;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;

import com.sfh.cpc.com.ImageSelector;
import com.sfh.lib.mvvm.service.BaseViewModel;
import com.sfh.lib.ui.AbstractLifecycleActivity;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 功能描述:权限处理
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/5/10
 */
public abstract class BasePermissionsActivity<VM extends BaseViewModel>  extends AbstractLifecycleActivity<VM> implements   EasyPermissions.PermissionCallbacks{

    public  static final int RC_CAMERA_AND_LOCATION = 0x1222;

    /***
     * 需要申请的权限
     * @return
     */
    public abstract String[] getPermissions();

    /***
     * 处理自己也业务
     */
    public  void  onPermissionsSuccess(){

    }

    /***
     * 检查是否拥有权限
     */
    @AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
    public void hasPermissions() {

        if (EasyPermissions.hasPermissions (BasePermissionsActivity.this, this.getPermissions())) {

            // 已经申请过权限，做想做的事
            this.onPermissionsSuccess();

        } else {
            // 没有申请过权限，现在去申请
            EasyPermissions.requestPermissions (BasePermissionsActivity.this, "拍照与图片选择使用",
                    RC_CAMERA_AND_LOCATION, this.getPermissions());
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult (requestCode, permissions, grantResults);
        //把申请权限的回调交由EasyPermissions处理
        EasyPermissions.onRequestPermissionsResult (requestCode, permissions, grantResults, this);
    }



    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        //权限申请被用户拒绝
        if (EasyPermissions.somePermissionPermanentlyDenied (this, perms)) {
            new AppSettingsDialog.Builder (this).build ().show ();
        }
    }
    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        // 已经申请过权限，做想做的事
        this.onPermissionsSuccess();
    }

}
